import { HttpException, HttpStatus, Injectable, Req } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { User, Message, Gender } from '../entities';

@Injectable()
export class TestService {
  constructor(
    @InjectRepository(User)
    private readonly usersRepository: Repository<User>,
    @InjectRepository(Message)
    private readonly message: Repository<Message>,
  ) {}

  async create() {
    const newUser = this.usersRepository.create({
      phone: `86${Math.floor(Math.random() * 10000000000)}`,
      name: `User_${Math.random().toString(36).substring(7)}`,
      height: 40,
      weight: 30,
      birthDate: '2024-01-01',
      education: '博士',
      comorbidities: ['None'],
      password: 'test',
      gender: Gender.MALE, // Use the correct Gender enum value
      // password: user.password,
    });

    await this.usersRepository.save(newUser);

    const findUser = await this.usersRepository.findOne({
      where: { phone: newUser.phone },
    });

    const newMessage = this.message.create({
      content: 'test',
      userId: findUser.userId,
    });

    await this.message.save(newMessage);
  }

  findUser() {
    return this.usersRepository.find({
      where: { isDeleted: false },
    });
  }

  findMessage() {
    return this.message.find({
      where: { isDeleted: false },
    });
  }

  removeUser(userId: number) {
    return this.usersRepository.delete({ userId });
  }

  removeMessage(messageId: number) {
    return this.message.delete({ messageId });
  }
}
